package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.gps;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CWeightSheets;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysDic;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl.CWeightSheetsServiceImpl;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl.SysDicServiceImpl;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

@Component
public class GpsBU {
    @Autowired
    private SysDicServiceImpl sysDicService;
    @Autowired
    private GetMds gpsMds;
    @Autowired
    private CWeightSheetsServiceImpl cWeightSheetsService;
//  设置超速阈值
    public SzResult setOverSpeedThreshold( String overSpeed,String plantId){
        SzResult result = new SzResult();
        QueryWrapper<SysDic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dic_type","gps18");
        queryWrapper.eq("dic_key","overspeed");
        SysDic sysDic = sysDicService.getOne(queryWrapper);
        if (sysDic == null){
            sysDic = new SysDic();
            sysDic.setDicType("gps18");
            sysDic.setDicKey("overspeed");
            sysDic.setDicVal(overSpeed);
            sysDic.setDicInfo("");
            sysDic.setDicDesc("定位设备超过多少速度就计入报警");
            sysDic.setDicBelong("");
            sysDic.setCacheFlag(null);
            sysDic.setDelFlag(0);
            sysDic.setDataFrom(plantId);
            sysDicService.save(sysDic);
        }
        sysDic.setId(sysDic.getId());
        sysDic.setDicVal(overSpeed);
        sysDicService.updateById(sysDic);
        return SzResult.Success();
    }
//    查询速度
    public SzResult getOverSpeedThreshold(String plantId){
        QueryWrapper<SysDic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dic_type","gps18");
        queryWrapper.eq("dic_key","overspeed");
        queryWrapper.eq("DATA_FROM",plantId);
        SysDic sysDic = sysDicService.getOne(queryWrapper);
        if (sysDic == null){
            return SzResult.Error("未设置");
        }
        return SzResult.success(sysDic.getDicVal());
    }

//    查询历史轨迹
//    school_id、lang、mds、objectid、isAutoplay、StartTime、EndTime
    public SzResult getHistoryTrack(String plantId,String weightSheetId){
        String platformUrl = "";
        String lang = "cn";
        String mds = "";
        String objectid = "";
        String school_id = "";
        String isAutoplay = "true";
        String startTime = "";
        String endTime = "";
      mds = gpsMds.getMds();
        school_id = gpsMds.getUserId();
     QueryWrapper<SysDic> queryWrapper = new QueryWrapper<>();
     queryWrapper.eq("DIC_TYPE","gps18");
     queryWrapper.eq("DIC_KEY","platformUrl");
     queryWrapper.eq("DATA_FROM",plantId);
     SysDic sysDic = sysDicService.getOne(queryWrapper);
     if (sysDic != null){
         platformUrl = sysDic.getDicVal();
     }else{
         return SzResult.Error("未设置定位平台参数platformUrl");
     }
//     cWeightSheetsService
        QueryWrapper<CWeightSheets> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("ID",weightSheetId);
        queryWrapper1.eq("WEISHT_DELETED",0);
        queryWrapper1.eq("DATA_FROM",plantId);
        CWeightSheets cWeightSheets = cWeightSheetsService.getOne(queryWrapper1);
        if (cWeightSheets == null){
            return SzResult.Error("未找到磅单");
        }else{
            // 假设 startTime 和 endTime 是从对象中获取的时间字符串
             startTime = cWeightSheets.getWeishtEmptyTm().toString().replace("T", " ");
             endTime = cWeightSheets.getWeishtFullTm().toString().replace("T", " ");

// 检查是否包含秒部分（通过长度判断）
            if (!startTime.contains(":") || startTime.split(":").length < 3) {
                startTime += ":00"; // 如果没有秒部分，拼接 ":00"
            }
            if (!endTime.contains(":") || endTime.split(":").length < 3) {
                endTime += ":59"; // 如果没有秒部分，拼接 ":59"
            }
            // 定义时间格式解析器
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

// 解析为 LocalDateTime
            LocalDateTime startDateTime = LocalDateTime.parse(startTime, formatter);
            LocalDateTime endDateTime = LocalDateTime.parse(endTime, formatter);

// 转换为时间戳（毫秒）
            long startTimestamp = startDateTime.toInstant(ZoneOffset.UTC).toEpochMilli();
            long endTimestamp = endDateTime.toInstant(ZoneOffset.UTC).toEpochMilli();
            startTime = String.valueOf(startTimestamp);
            endTime = String.valueOf(endTimestamp);
            if (cWeightSheets.getWeishtGpsDevId() == null){
                return SzResult.Error("未找到设备号，此单未进行定位");
            }
            objectid = cWeightSheets.getWeishtGpsDevId();
        }
//组成http请求
        String result = platformUrl+"?"+"school_id="+school_id+"&"+"lang="+lang+"&"+"mds="+mds+"&"+"objectid="+objectid+"&"+"isAutoplay="+isAutoplay+"&"+"StartTime="+startTime+"&"+"EndTime="+endTime;
        return SzResult.success(result);
    }

}
